<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="../assets/js/vue.js"></script>
    <title>V-for 案例</title>
</head>

<body>
    <h1>v-for指令用法</h1>
    <hr>

    <div id="app">
        <ul>
            <li v-for="item in sortItems">{{ item }}</li>
        </ul>
        <br/>
        <ul>
            <li v-for="(student,index) in sortStudent">{{ index }}: {{ student.name }}: {{ student.age }}</li>
        </ul>
    </div>
    <script>
        var app = new Vue({
            el: '#app',
            data: {
                items:[20,23,18,65,32,19,5,56,41],
                students:[
                    {name:'jspang',age:32},
                    {name:'Panda',age:30},
                    {name:'PanPaN',age:21},
                    {name:'King',age:45}
                ]
            },
            computed:{
                sortItems:function(){
                    return this.items.sort(sortNumber)
                },
                sortStudent:function(){
                    return sortByKey(this.students,'age')
                }
            }
        })
        function sortNumber(a,b){
            return a-b
        }
        function sortByKey(array,key){
            return array.sort(function(a,b){
                var x=a[key];
                var y=b[key];
                return ((x<y)?-1:((x>y)?1:0));
            })
        }
    </script>
</body>

</html>